<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use app\validate\Submit;
use app\wx_pay\controller\JsApiPayV3;
class Index extends BaseController
{

	protected $userName = 'xxxx';
    protected $password = 'xxxx';//登录密码：lucky123
    protected $redis_key = 'xxxx.yyuebd.com-send_code-';
	protected $redis_time = 300;//redis有效时间，即短信有效时间(秒)
	protected $interval_time = 120;//提示间隔时间后再发送(秒)
	
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }
    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
    //获取科目
    public function getProject(){
        $project = Db::table('project')
        ->filter(function($userData){
            if($userData['project_virtually_status'] == 1){
                $sum = $userData['project_virtually_num'];
            }else{
               $sum = Db::table('submit')
                 ->alias('s')
                ->join('sign_order o','s.submit_id = o.uid')
                ->lock(true)
             ->where('project_id',$userData['project_id'])
             ->where('ispay',1)
             ->where('del_status',0)
             ->count();   
            }
            $userData['b_num'] = $sum;
            //$userData['s_num'] =  $userData['project_num']- $sum;
            $userData['s_num'] =  99999;
            return $userData;
        })
        ->select();
        return json([
            'status'=>200,
            'data' =>$project,
            'move_time' =>10,
        ]);
    }
    //获取轮播图
    public function getSwiper(){
        $picture1 = Db::table('picture1')->select();
        $picture2 = Db::table('picture2')->select();
        return json([
            'status'=>200,
            'data1' =>$picture1,
            'data2' =>$picture2,
        ]);
    }    
    /**
     *@param $type 哪种活动
     * @param $picture_id string 哪个项目
     * @throws \think\db\exception\DbException
     */
    //获取报名成功人数方法
    private function getPaySuccessPeople(int $type, string $picture_id=''){
        if($type == 1){
            //获取限制总人数
            $view_nums = Db::table('picture')->where('picture_id',$picture_id)->value('picture_info');
            return $view_nums - Db::table('submit')
                ->alias('s')
                ->join('sign_order o','s.submit_id = o.uid')
                ->lock(true)
                ->where([
                    ['s.view_id','=',1],['s.picture_id','=',$picture_id],
                    ['o.ispay','=',1],['s.del_status','=',0]
                ])->count();
        }else{
            $view_nums = Db::table('view')->where('view_id',2)->value('view_nums');
            return $view_nums -  Db::table('submit')
                ->alias('s')
                ->join('sign_order o','s.submit_id = o.uid')
                ->lock(true)
                ->where([
                    ['s.view_id','=',2],
                    ['o.ispay','=',1],
                    ['s.del_status','=',0]
                ])->count();
        }

    }
    public function submit(){
        $data = $this->request->param();
        $submit = new Submit();
        if($submit->check($data)){
            //限制人数
             $project = Db::table('project')->where('project_id',$data['project_id'])->find();
             $project_submit_num = Db::table('submit')
                 ->alias('s')
                ->join('sign_order o','s.submit_id = o.uid')
                ->lock(true)
             ->where('project_id',$data['project_id'])
             ->where('ispay',1)
             ->where('del_status',0)
             ->count();
            if($project['project_num'] - $project_submit_num<=0){
                return json([
                   'status'=>500,
                   'msg'=>'当前项目已报满'
                ]);
            }
            $data['submit_time'] = date("Y-m-d H:i:s");
            $submit_id = Db::table('submit')->insertGetId($data);
            if($submit_id){
                $money = $project['project_money'];
                $JsApiPayV3 = new JsApiPayV3();
                $res = $JsApiPayV3->pay('applet','学生报名费用',genAppNo(),$money,$data['wx_openid'],time(),$submit_id,'');
                return json($res);
            }else{
                return json(['status'=>500,'msg'=>'提交失败']);
            }
        }else{
            return json(['status'=>500,'msg'=>$submit->getError()]);
        }
    }
    public function getPhone(){
        return json(['status'=>200,'data'=>'18888888888']);
    }
    public function myDetails(){
        $data = request()->param();
        $wx_openid = $data['wx_openid'];
        $page = $data['page'];
        $limit = $data['limit'];
        $res = Db::table('submit')
        ->alias('s')
        ->join('project p','s.project_id = p.project_id')
        ->filter(function($data){
            $ispay = [0=>'未支付',1=>'已支付'];
            $sign_order = Db::table('sign_order')->where('uid',$data['submit_id'])->find();
            $data['out_trade_no'] = $sign_order['out_trade_no'];
            $ispay_text = $ispay[$sign_order['ispay']];
            $data['ispay'] = $sign_order['ispay'] == 1?"<p style='color:#ff0000'>{$ispay_text}</p>":"<p style='color:#44e015'>{$ispay_text}</p>";
            return $data;
        })
        ->where('wx_openid',$wx_openid)
        ->page($page,$limit)
        ->order('submit_id desc')
        ->select()->toArray();
        $total = Db::table('submit')
        ->alias('s')
        ->join('project p','s.project_id = p.project_id')
        ->where('wx_openid',$wx_openid)
        ->count();
        // $res = [];
        // $total = 0;
        return json(['status'=>200,'msg'=>'ok','data'=>$res,'total'=>$total,'noListMsg'=>'暂无数据！']);
    }
    // public function submit(){
    //     $data = $this->request->param();
    //     $submit = new Submit();
    //     $activity = Db::table("activity")->where('activity_id',$data['view_id'])->find();
    //     $time = time();
    //     if (strtotime($activity['date1'])<$time && $time<strtotime($activity['date2'])){
    //         if($data['view_id'] == 1) $check = $submit->check($data);else $check = $submit->scene('v2')->check($data);
    //         if($check){
    //             //限制人数
    //             if($this->getPaySuccessPeople($data['view_id'],$data['view_id'] == 1?$data['picture_id']:'')<=0){
    //                 return json(['status'=>500,'msg'=>'当前报名人数已满']);
    //             }
    //             $data['submit_time'] = date("Y-m-d H:i:s");
    //             $uid = Db::table('submit')->insertGetId($data);
    //             if(!$uid)return json(['status'=>500,'msg'=>'数据提交失败']);
    //              $out_trade_no = genAppNo();
    //             if ($data['view_id'] == 1){
    //                 $money =  Db::table('picture')->where('picture_id',$data['picture_id'])->value('picture_money');
    //                 //若是特殊学生
    //                 if($data['special_status'] == 1){
    //                     Db::table('sign_order')->insert(['out_trade_no'=>$out_trade_no,'ispay'=>1,'pay_time'=>date('Y-m-d H:i:s',$time),'uid'=>$uid,'pay_price'=>0.00,'add_time'=>date('Y-m-d H:i:s',$time)]);
    //                     return json(['status'=>200,'msg'=>'报名成功！']);
    //                 }
    //             }else{
    //                 $money =  Db::table('view')->where([
    //                     ['view_id','=',2]
    //                 ])->value('view_money');
    //                 if($money <= 0){
    //                     Db::table('sign_order')->insert(['out_trade_no'=>$out_trade_no,'ispay'=>1,'pay_time'=>date('Y-m-d H:i:s',$time),'uid'=>$uid,'pay_price'=>0.00,'add_time'=>date('Y-m-d H:i:s',$time)]);
    //                     return json(['status'=>200,'msg'=>'报名成功！']);
    //                 }
    //             }
    //             $wxs_openid = Db::table('wxs_user')->where('wxs_id',$data['wxs_id'])->value('wxs_openid');
    //             $JsApiPayV3 = new JsApiPayV3();
    //             $res = $JsApiPayV3->pay('applet','学生报名费用',$out_trade_no,$money,$wxs_openid,$time,$uid,'');
    //             if($res){
    //                 //msg支付成功后才弹窗
    //                 return json(['status'=>200,'msg'=>'支付成功','data'=>$res]);
    //             }else{
    //                 return json(['status'=>500,'msg'=>'提交失败']);
    //             }
    //         }else{
    //             return json(['status'=>500,'msg'=>$submit->getError()]);
    //         }
    //     }else{
    //         return json(['status'=>500,'msg'=>'不在时间范围内']);
    //     }

    // }
}
